<?php
/*
 * @Author: CYH
 * @Date: 2021-03-15 10:17:45
 * @LastEditTime: 2021-04-23 16:00:01
 * @LastEditors: Please set LastEditors
 * @Description: HCM->宿舍管理系统对接model类
 * @FilePath: \dorm_manage_DEV\application\models\Hcm\Hcm_manage.php
 */

    //HCM->宿舍管理系统对接model类 
    class Hcm_manage extends CI_Model
    {
        // 构造方法
        public function __construct()
        {

            parent::__construct();
            $this->load->database();
        }

        //部门岗位变更HCM->宿舍管理系统
        public function hcm_update($jobNumber, $department, $job)
        {
            $data = array(
                'jobNumber' => $jobNumber,
                'department' => $department,
                'job' => $job
            );
            //更新床位部门岗位信息
            $this->db->where('jobNumber', $jobNumber);
            $this->db->update('dorm_bed', $data);
            $bed_update = $this->db->affected_rows();
            // print_r($this->db->last_query());
            //更新人员信息表部门岗位信息
            $this->db->where('jobNumber', $jobNumber);
            $this->db->update('dorm_livepeople', $data);
            $livepeople_update = $this->db->affected_rows();
            // print_r($this->db->last_query());

            if($bed_update && $livepeople_update)
            {
                $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => $jobNumber);
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '更新失败' ,'data' => $jobNumber);
                $result = json_encode($return_fa);
            }
            return $result;
        }

        // //入住，退宿信息更新HCM->宿舍管理系统
        // public function hcm_live_retreat($date)
        // {
        //     // 取当天的入住,退宿人的信息
        //     $query = $this->db->query("SELECT jobNumber, name, liveDate, retreatDate FROM dorm_stay_retreat_tune WHERE liveDate = '".$date."' OR retreatDate = '".$date."'");
        //     if($query)
        //     {
        //         $return_su = array("res" => '40000', "msg" => '查询成功', "data" => $query->result());
        //         $result = json_encode($return_su, JSON_UNESCAPED_UNICODE);
        //     }
        //     else
        //     {
        //         $return_fa = array("res" => '40001', "msg" => '无符合条件的数据' ,"data" => '');
        //         $result = json_encode($return_fa, JSON_UNESCAPED_UNICODE);
        //     }
        //     return $result;
        // }
        
        /**
         * @description:取需要更新到HCM的人员的入住信息 
         * @param {*}
         * @return {*}
         */
        public function hcm_live()
        {
            // $query = $this->db->get_where('dorm_stay_retreat_tune', array('type' => 1, 'hcmUpdate' => ''));
            // $query = $this->db->get_where('dorm_stay_retreat_tune', array('type' => 1, 'jobNumber !='  => '', 'hcmUpdate' => ''));
            // $query = $this->db->get_where('dorm_livepeople', array('jobNumber' => '11138'));
            $query = $this->db->query("SELECT A.jobNumber, A.name, A.liveDate, B.repetitionCheck FROM dorm_stay_retreat_tune AS A LEFT JOIN dorm_livepeople AS B ON A.jobNumber = B.jobNumber AND A.idNumber = B.idNumber
                                        WHERE type = 1 AND hcmUpdate = '' AND liveCheck = 'X' ");
                                        
            if(count($query->result())>0)
            {
                $return_su = array("res" => '40000', "msg" => '查询成功', "data" => $query->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array("res" => '40001', "msg" => '查询无数据' ,"data" => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description:取重复更新人员数据 
         * @param {idNumber}
         * @return {*}
         */
        public function repetitionCheck($idNumber)
        {
            $query = $this->db->get_where('dorm_livepeople', array('idNumber' => $idNumber, 'repetitionCheck' => 'X'));
            if($query->num_rows()>0)
            {
                $return_su = array("res" => '40000', "msg" => '查询成功', "data" => $query->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array("res" => '40001', "msg" => '查询无数据' ,"data" => '');
                $result = json_encode($return_fa);
            }
        }

        /**
         * @description:取需要更新到HCM的人员的退宿信息 
         * @param {*}
         * @return {*}
         */
        public function hcm_retrea()
        {
            $query = $this->db->query("SELECT A.jobNumber, A.name, A.retreatDate, B.repetitionCheck FROM dorm_stay_retreat_tune AS A LEFT JOIN dorm_livepeople AS B ON A.jobNumber = B.jobNumber AND A.idNumber = B.idNumber
                                        WHERE type = 0 AND hcmUpdate = '' AND quitCheck = 'X' GROUP BY jobNumber");
            // $query = $this->db->get_where('dorm_stay_retreat_tune', array('type' => 0, 'hcmUpdate' => ''));
            // $query = $this->db->get_where('dorm_stay_retreat_tune', array('type' => 0, 'jobNumber !='  => '', 'hcmUpdate' => ''));
            // $query = $this->db->get_where('dorm_livepeople', array('room_number' => '459'));
            // $query = $this->db->query("SELECT * FROM dorm_stay_retreat_tune AS A LEFT JOIN dorm_livepeople AS B ON A.idNumber = B.idNumber AND B.repetitionCheck = 'X' 
            //                             WHERE A.type = 0 AND A.hcmUpdate = ''");
            if(count($query->result())>0)
            {
                $return_su = array("res" => '40000', "msg" => '查询成功', "data" => $query->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array("res" => '40001', "msg" => '查询无数据' ,"data" => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }
        
        /**
         * @description:取入住人员 
         * @param {*}
         * @return {*}
         */
        public function dorm_livePeople()
        {
            $query = $this->db->get_where('dorm_bed', array('check_inWhether' => '1'));
            if($query)
            {
                $return_su = array("res" => '40000', "msg" => '查询成功', "data" => $query->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array("res" => '40001', "msg" => '无符合条件的数据' ,"data" => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description: HCM->宿舍管理系统信息导入
         * @param {jobNumber, idNumber, department1, department2, department3, job, phone, location}
         * @return {*}
         */
        public function hcm_peopleInformation($jobNumber = FALSE, $idNumber = FALSE, $department1 = FALSE, $department2 = FALSE, $department3 = FALSE, $job = FALSE, $phone = FALSE, $location = FALSE)
        {
            //更新床位表
            $bed = array(
                'idNumber' => $idNumber,
                'department1' => $department1,
                'department2' => $department2,
                'department3' => $department3,
                'job' => $job,
                'phone' => $phone,
            );

            $this->db->where('jobNumber', $jobNumber);
            $this->db->update('dorm_bed', $bed);
            $query_bed = $this->db->affected_rows();

            $livepeople = array(
                'idNumber' => $idNumber,
                'department1' => $department1,
                'department2' => $department2,
                'department3' => $department3,
                'job' => $job,
                'location' => $location,
                'phone' => $phone
            );

            $this->db->where('jobNumber', $jobNumber);
            $this->db->update('dorm_livepeople', $livepeople);
            $query_livepeople = $this->db->affected_rows();

            if($query_bed && $query_livepeople)
            {
                $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => $idNumber);
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '更新失败' ,'data' => $idNumber);
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description:需进行工号更新的人员传到HCM 
         * @param {*}
         * @return {*}
         */
        public function jobnumber_update()
        {
            $query = $this->db->query("SELECT idNumber FROM dorm_bed WHERE jobNumber = '' AND idNumber != '' AND check_inWhether = '1'");
            // $query = $this->db->query("SELECT idNumber FROM dorm_bed WHERE room_number = '301' AND check_inWhether = '1' AND idNumber != ''");
            if(count($query->result())>0)
            {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '查询无数据' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description:HCM->宿舍系统工号更新 
         * @param {idNumber, jobNumber}
         * @return {*}
         */
        public function hcm_jobNumberUpdate($idNumber, $jobNumber)
        {
            //更新记录表数据
            // $this->db->query("UPDATE dorm_stay_retreat_tune SET jobNumber = '".$jobNumber."' WHERE idNumber = '".$idNumber."' AND jobNumber = '' AND type = ''");
            $this->db->query("UPDATE dorm_stay_retreat_tune SET jobNumber = '".$jobNumber."' WHERE idNumber = '".$idNumber."' AND jobNumber = ''");
            $record = $this->db->affected_rows();

            //更新人员信息表
            $this->db->query("UPDATE dorm_livepeople SET jobNumber = '".$jobNumber."' WHERE idNumber = '".$idNumber."' AND jobNumber = '' AND liveCheck != ''");
            $livePeople = $this->db->affected_rows();

            //更新床位表
            $this->db->query("UPDATE dorm_bed SET jobNumber = '".$jobNumber."' WHERE idNumber = '".$idNumber."' AND jobNumber = ''");
            $bed = $this->db->affected_rows();
            
            $arr = array(
                'jobNumber' => $jobNumber,
                'idNumber' => $idNumber
            );

            if($record && $livePeople && $bed)
            {
                $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => $arr);
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '更新失败' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description:宿舍管理系统->HCM同步标识更新 
         * @param {arr}
         * @return {*}
         */
        public function hcmUpdate($arr)
        {
            $this->db->update_batch('dorm_stay_retreat_tune', $arr, 'jobNumber');
            $query = $this->db->affected_rows();
            if($query)
            {
                $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => '');
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '更新失败' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description:HCM花名册信息存入宿舍管理系统数据库 
         * @param {arr}
         * @return {*}
         */
        public function hcmUpdateRoster($arr)
        {
            $roster = new stdClass();
            $roster_arr = [];
            for($i=0; $i<count($arr); $i++){    
                $roster->jobNumber = $arr[$i]->PERNR;
                $roster->name = $arr[$i]->NACHN;
                $roster->sex = $arr[$i]->GESCH;
                $roster->department1 = $arr[$i]->ZONE_STEXT;
                $roster->department2 = $arr[$i]->ZTWO_STEXT;
                $roster->department3 = $arr[$i]->ZTHREE_STEXT;
                $roster->department4 = $arr[$i]->ZFOUR_STEXT;
                $roster->department5 = $arr[$i]->ZFIVE_STEXT;
                $roster->department6 = $arr[$i]->ZSIX_STEXT;
                $roster->birthDate = $arr[$i]->GBDAT;
                $roster->maritalStatus = $arr[$i]->FTEXT;
                $roster->entryDate = $arr[$i]->ZRZ_DATE;
                $roster->resignationDate = $arr[$i]->BEGDA;
                $roster->lengthService = $arr[$i]->ZGL;
                $roster->job = $arr[$i]->ZPLANS_TEXT;
                $roster->phone = $arr[$i]->TEL;
                $roster->idNumber = $arr[$i]->ICNUM;
                $roster_arr[$i] = $roster;
                array_push($roster_arr, $roster);
                $roster = null;
                $roster = new stdClass();
            }

            //更新花名册表
            $this->db->insert_batch('dorm_roster', $roster_arr);
            $rosterUpdate = $this->db->affected_rows();

            if($rosterUpdate)
            {
                $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => '');
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '更新失败' ,'data' => '');
                $result = json_encode($return_fa);
            }

            return $result;
        }

        /**
         * @description:从宿舍管理系统数据库取花名册分页数据
         * @param {arr}
         * @return {*}
         */
        public function hcmRosterPagination($limit)
        {
            if($limit != '')
            {
                $limit1 = ("$limit" - 1) * 10;
            }
    
            $sj1 = $limit == '' ? ' 1 = 1 ' : " Limit " . $limit1 . ",10 "; 

            $sql = "SELECT * FROM dorm_roster".$sj1;

            $roster_data = $this->db->query($sql, $sj1);

            if($roster_data->num_rows()>0)
            {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $roster_data->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '查询无数据' ,'data' => '');
                $result = json_encode($return_fa);
            }

            return $result;
        }

        /**
         * @description:从宿舍管理系统数据库取全部数据
         * @param {arr}
         * @return {*}
         */
        public function hcmRosterPaginationTotal()
        {

            $sql = "SELECT * FROM dorm_roster";

            $roster_data = $this->db->query($sql);

            if($roster_data->num_rows()>0)
            {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $roster_data->result());
                $result = json_encode($return_su);
            }
            else
            {
                $return_fa = array('res' => '40001', 'msg' => '查询无数据' ,'data' => '');
                $result = json_encode($return_fa);
            }

            return $result;
        }

        /**
         * @description:从宿舍管理系统数据库取数据用于EXCEL导出
         * @param {arr}
         * @return {*}
         */
        public function hcm_people_information_excle()
        {
            $query = $this->db->query("SELECT * FROM dorm_roster");
            $result = $query->result();
            return json_encode(array('ret' => '40001', 'msg' => $result));
        }

    }
